<template>
  <el-container>
    <el-header>
      <el-form :inline="true" style="margin-top: 20px">
        <el-form-item label="关键字">
          <el-input v-model="queryObject.parameters.keyword" placeholder="关键字" clearable @clear="clearConditions" ></el-input>
        </el-form-item>
        <el-form-item label="消息接收时间">
          <el-date-picker v-model="dateRange" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" v-on:click="query"></el-button>
        </el-form-item>
      </el-form>
    </el-header>
    <el-main>
      <el-table height="100%" :data="dataTable.result.rows" border style="width: 100%" highlight-current-row>
        <el-table-column label="标题" min-width="200" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-button type="text" @click="view(scope.row)" :title="scope.row.dataItem.title">
              {{ scope.row.dataItem.title }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="发件人" width="200">
          <template slot-scope="scope">[{{ scope.row.dataItem.senderId }}]{{ scope.row.dataItem.senderName }}</template>
        </el-table-column>
        <el-table-column label="时间" header-align="center" align="center" width="200">
          <template slot-scope="scope">{{ scope.row.dataItem.receiveTime }} </template>
        </el-table-column>
        <el-table-column header-align="center" align="center" width="80" fixed="right">
          <template slot="header">
            <el-button type="text" icon="el-icon-refresh" @click="query" title="刷新" ></el-button>
          </template>
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-delete" @click="remove(scope.row)" title="删除"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <el-footer>
      <el-pagination v-on:current-change="query" :current-page.sync="queryObject.pageIndex" :page-size="queryObject.pageSize" layout="total, prev, pager, next" :total="dataTable.rowCount"></el-pagination>
    </el-footer>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      queryObject: {
        pageIndex: 1,
        pageSize: 20,
        sortFields: {
          receiveTime: true
        },
        parameters: {
          keyword: null,
          beginTime: null,
          endTime: null
        }
      },
      dataTable: {
        result: [
        ],
        rowCount: 0
      }
    }
  },
  computed: {
    dateRange: {
      get(){
        if (this.queryObject.parameters.beginTime && this.queryObject.parameters.endTime)
          return [this.queryObject.parameters.beginTime, this.queryObject.parameters.endTime];
        else
          return [];
      },
      set(val){
        if (val && val.length > 0){
          this.queryObject.parameters.beginTime = val[0];
          this.queryObject.parameters.endTime = val[1];
        }
        else{
          this.queryObject.parameters.beginTime = null;
          this.queryObject.parameters.endTime = null;
        }
      }
    }
  },
  mounted() {
    this.query();
  },
  methods: {
    query(){
      this.$service.data.pagedquery({routeData:{name:'UserMessageList'}, data: this.queryObject}).then(r => {
        if (r.success)
          this.dataTable = r.data;
      });
    },
    clearConditions() {
      this.query();
    },
    remove(row) {
      confirm('确认要删除所选项吗？请谨慎操作！', '警告', 'warning', () => {
        let data = [ {messageId: row.dataItem.messageId}];
        this.$service.data.dataOperate({routeData:{name:'UserMessageRemove'}, data:data}).then((response) => {
          if (!response.success) {
            let msg = response.message || "操作失败";
            alert(msg, "消息", "error");
          }
          else{
            this.query();
          }
        });
      });
    },
    view(row) {
      this.$router.push({ name: 'userMessageItem', params: { id: row.dataItem.messageId } })
    },
  }
}
</script>